package com.sky.controller.admin;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;
import java.util.List;
import java.util.Set;

@RestController
@Slf4j
@RequestMapping("/admin/dish")
@Api(tags = "菜品相关接口")
@RequiredArgsConstructor
public class DishControlller {

    // 注入DishService
    private final DishService dishService;
    private final RedisTemplate redisTemplate;

    // 新增加菜品
    @PostMapping
    @ApiOperation("新增加菜品")
    public Result save(@RequestBody DishDTO dishDTO){
        // 记录新增菜品的信息
        log.info("新增菜品:{}",dishDTO);
        // 调用dishService的saveWithFlavor方法，保存菜品信息
        dishService.saveWithFlavor(dishDTO);

        String key="dish_id"+dishDTO.getCategoryId();
        cleanCache(key);

        // 返回成功结果
        return Result.success();
    }

    // 菜品分类查询
    @GetMapping("/page")
    @ApiOperation("菜品分类查询")
    public Result<PageResult> page(DishPageQueryDTO dishPageQueryDTO){
        // 记录分页查询菜品的信息
        log.info("分页查询菜品:{}",dishPageQueryDTO);
        // 调用dishService的pageQuery方法，传入dishPageQueryDTO参数，获取分页结果
        PageResult pageResult=dishService.pageQuery(dishPageQueryDTO);
        // 返回分页结果
        return Result.success(pageResult);
    }


    // 根据id删除菜品
    @DeleteMapping
    @ApiOperation("菜品批量删除")
    public Result delete(@RequestParam List<Long> ids){
        log.info("菜品批量删除：{}",ids);
        dishService.deleteBatch(ids);

        cleanCache("dish_*");

        return Result.success();
    }

    @GetMapping("/{id}")
    @ApiOperation("根据id查询菜品")
    public Result<DishVO> getById(@PathVariable Long id){
        log.info("根据id查询菜品：{}",id);
        DishVO dishVO=dishService.getByIdwithFlavor(id);
        return Result.success(dishVO);
    }
    @PutMapping
    @ApiOperation("修改菜品")
    public Result update(@RequestBody DishDTO dishDTO){
        log.info("修改菜品：{}",dishDTO);
        dishService.updateWithFlavor(dishDTO);

        cleanCache("dish_*");

        return Result.success();
    }

    @PostMapping("/status/{status}")
    @ApiOperation("菜品启售与停售")
    public Result stopOrStop(@PathVariable Integer status,Long id){
        log.info("菜品启售与停售：{}",status);
        dishService.starOrStop(status,id);

        cleanCache("dish_*");
//        Set keys = redisTemplate.keys("dish_*");
//        redisTemplate.delete(keys);
        return Result.success();
    }
    @GetMapping("/list")
    @ApiOperation("根据分类id查询菜品")
    public Result<List<Dish>> list(Long categoryId){
        List<Dish> list = dishService.list(categoryId);
        return Result.success(list);
    }


// 清除缓存
    private void cleanCache(String pattern){
        // 获取符合pattern的所有key
        Set keys=redisTemplate.keys(pattern);
        // 删除所有符合pattern的key
        redisTemplate.delete(keys);
    }

}
//    dockers run --restart=always --privileged=true --name redis-container \
//    -v /home/redis/myredis/myredis.conf:/etc/redis/redis.conf \
//    -v /home/redis/myredis/data:/data \
//    -p 6379:6379 -d redis redis-server /etc/redis/redis.conf
//dockers run \
//--restart=always \
//--log-opt max-size=100m \
//--log-opt max-file=2 \
//-p 6379:6379 \
//--name redis \
//-v /home/redis/myredis/myredis.conf:/etc/redis/redis.conf \
//-v /home/redis/myredis/data:/data \
//-d redis:alpine:3.17 redis-server /etc/redis/redis.conf \
//--appendonly yes \
//--requirepass 123456
//        [root@bogon ~]# docker run --name redis -d -p 6379:6379 -v /opt/redis/data:/data --restart=always --privileged=true redis:7.2.3
//        ec298ad7305a6176487cfedd55579e1429455ec5d12d295f4fa14bcc770ad707
//        [root@bogon ~]#
//        docker run --name redis -d -p 6379:6379 -v /opt/redis/data:/data --restart=always --privileged=true redis:7.2.3